const socketServer = (server) => {
  const userList = new Map();
  server.on("connection", (socket) => {
    // 加入
    socket.on("join", (name) => {
      if (!name) return;
      userList.set(socket.id, name);
      socket.name = name;
      socket.broadcast.emit("welcome", name, userList.size, socket.id);
      socket.emit("welcome", name, userList.size, socket.id);
    });

    // 广播信息
    socket.on("message", (data) => {
      socket.broadcast.emit("gbmsg", data);
    });
    // 断开连接
    socket.on("disconnect", () => {
      console.log("quit", socket.id);
      const bool = userList.delete(socket.id);
      bool && socket.broadcast.emit("quit", socket.name, userList.size);
    });
  });
};

module.exports = { socketServer };
